Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

CSS 衍生的資安問題(下) - 我愛偷甚麼就偷甚麼

CSS 衍生的資安問題(下) - 我愛偷甚麼就偷甚麼

我知道你懂 hoisting,可是你了解到多深?

我知道你懂 hoisting,可是你了解到多深?

[JS102] Jest

[JS102] Jest






留言討論